//
//  BaseViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "BaseViewController.h"
#import "MBProgressHUD+Category.h"

@interface BaseViewController ()
@end

@implementation BaseViewController
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//
//    self.navigationController.delegate = self;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_APP_BACKGROUND;
    self.navigationHeight = NavigationBarHeight;
    self.bottomSafeHeight = SafeAreaBottomHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded && !self.view.window) {
        self.view = nil;
    }
}

//隐藏导航栏
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    [navigationController setNavigationBarHidden:YES animated:NO];
//}

- (void)dealloc {
    NSLog(@"销毁:%@",NSStringFromClass([self class]));
}

/// alert提示操作【单个action】
- (void)alertViewTitle:(NSString*)title message:(NSString*)msg title:(NSString*)buttonTitle block:(CycBlock)block {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title?:@"" message:msg?:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:buttonTitle
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (block) {
                                                                 block();
                                                             }
                                                         }];
    [alertController addAction:cancelAction];
    [[self mainWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
}

/// alert提示操作【左右双action】
- (void)alertViewTitle:(NSString*)title message:(NSString*)msg left:(NSString*)left leftBlock:(CycBlock)leftBlock right:(NSString*)right  rightBlock:(CycBlock)rightBlock  {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title?:@"" message:msg?:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:left?:@""
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (leftBlock) {
                                                                 leftBlock();
                                                             }
                                                         }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:right?:@""
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (rightBlock) {
                                                             rightBlock();
                                                         }
                                                     }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [[self mainWindow].rootViewController presentViewController:alertController animated:YES completion:nil];
}

//获取当前的window
- (UIWindow *)mainWindow {
    UIApplication *app = [UIApplication sharedApplication];
    return [app.delegate window];
}

//显示加载中...
-(void)showLoadingViewWithText:(NSString *__nullable)text {
    [MBProgressHUD showMessage:text];
}
//移除加载中...
-(void)removeLoadingView {
    [MBProgressHUD hideHUD];
}

- (void)showSuccessWithText:(NSString *_Nullable)success {
    [MBProgressHUD showSuccess:success];
}
- (void)showErrorWithText:(NSString *_Nullable)error {
    [MBProgressHUD showError:error == nil ? @"" : error];
}

@end
